package net.arvin.utils;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;

import android.content.Context;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;

public class HttpRequestClient {
	private static final String BASE_URL = "http://192.168.1.100:8080/MobileBankSystem/";
	private static final String USER_NAME_KEY = "username";
	private static final String USER_QUESTION_KEY = "question";
	private static final String USER_ANSWER_KEY = "answer";
	private static final String USER_JSON_KEY = "user";
	private static final String PASSWORD_KEY = "password";
	private static final String CARD_JSON_KEY = "card";
	private static final String PHONE_NUMBER = "phonenumber";
	private static final String CARD_NUMBER = "cardnumber";
	private static final String RECHARGE_KEY = "recharge";
	private static final String TRANSFER_KEY = "transfers";
	private static final String CODEINFO_KEY = "code_info";
	private static final String UUID_KEY = "id";

	private static String getBaseUrl() {
		return BASE_URL;
	}

	private static String getLoginUrl() {
		return getBaseUrl() + "LoginServlet";
	}

	private static String getRegisterUrl() {
		return getBaseUrl() + "RegisterServlet";
	}

	private static String getUserInfoUrl() {
		return getBaseUrl() + "GetUserInfoServlet";
	}

	private static String getResetPSWUrl() {
		return getBaseUrl() + "ResetPSWServlet";
	}

	private static String getSaveUserUrl() {
		return getBaseUrl() + "SaveUserServlet";
	}

	private static String getTransferMoneyUrl() {
		return getBaseUrl() + "TransferMoneyServlet";
	}

	private static String getBindCardUrl() {
		return getBaseUrl() + "BindCardServlet";
	}

	private static String getAllCardsUrl() {
		return getBaseUrl() + "GetAllCardsServlet";
	}

	private static String getTransfersByUserUrl() {
		return getBaseUrl() + "GetTransfersByUser";
	}

	private static String getUnbindCardUrl() {
		return getBaseUrl() + "DeleteCardServlet";
	}

	private static String getPayCodeUrl() {
		return getBaseUrl() + "PayCodeServlet";
	}

	public static String getSweepUrl() {
		return getBaseUrl() + "SweepServlet";
	}
	private static String getRechargeUrl() {
		return getBaseUrl() + "RechargeServlet";
	}
	private static String getBillsUrl() {
		return getBaseUrl() + "BillsServlet";
	}

	public static void startPostLoginRequest(final String userName,
			final String password, final AsyncHttpResponseHandler handler,
			final Context context) {
		final List<NameValuePair> data = new ArrayList<NameValuePair>();
		data.add(new BasicNameValuePair(USER_NAME_KEY, userName));
		data.add(new BasicNameValuePair(PASSWORD_KEY, password));
		try {
			AsyncHttpClientUtil.post(getAsyncHttpClient(context),
					getLoginUrl(), context, new UrlEncodedFormEntity(data,
							HTTP.UTF_8), handler);
		} catch (final UnsupportedEncodingException e) {
			Toast.makeText(context, "网络连接异常，请检查", Toast.LENGTH_SHORT).show();
		}
	}

	public static void startPostRegisterRequest(final String user,
			final AsyncHttpResponseHandler handler, final Context context) {
		final List<NameValuePair> data = new ArrayList<NameValuePair>();
		data.add(new BasicNameValuePair(USER_JSON_KEY, user));
		try {
			AsyncHttpClientUtil.post(getAsyncHttpClient(context),
					getRegisterUrl(), context, new UrlEncodedFormEntity(data,
							HTTP.UTF_8), handler);
		} catch (final UnsupportedEncodingException e) {
			Toast.makeText(context, "网络连接异常，请检查", Toast.LENGTH_SHORT).show();
		}
	}

	public static void startPostGetUserInfoRequest(final String phonenumber,
			final AsyncHttpResponseHandler handler, final Context context) {
		final List<NameValuePair> data = new ArrayList<NameValuePair>();
		data.add(new BasicNameValuePair(PHONE_NUMBER, phonenumber));
		try {
			AsyncHttpClientUtil.post(getAsyncHttpClient(context),
					getUserInfoUrl(), context, new UrlEncodedFormEntity(data,
							HTTP.UTF_8), handler);
		} catch (final UnsupportedEncodingException e) {
			Toast.makeText(context, "网络连接异常，请检查", Toast.LENGTH_SHORT).show();
		}
	}

	public static void startPostResetPSWRequest(final String phonenumber,
			final String password, final String question, final String answer,
			final AsyncHttpResponseHandler handler, final Context context) {
		final List<NameValuePair> data = new ArrayList<NameValuePair>();
		data.add(new BasicNameValuePair(PHONE_NUMBER, phonenumber));
		data.add(new BasicNameValuePair(PASSWORD_KEY, password));
		data.add(new BasicNameValuePair(USER_QUESTION_KEY, question));
		data.add(new BasicNameValuePair(USER_ANSWER_KEY, answer));
		try {
			AsyncHttpClientUtil.post(getAsyncHttpClient(context),
					getResetPSWUrl(), context, new UrlEncodedFormEntity(data,
							HTTP.UTF_8), handler);
		} catch (final UnsupportedEncodingException e) {
			Toast.makeText(context, "网络连接异常，请检查", Toast.LENGTH_SHORT).show();
		}
	}

	public static void startPostSaveUserRequest(final String user,
			final AsyncHttpResponseHandler handler, final Context context) {
		final List<NameValuePair> data = new ArrayList<NameValuePair>();
		data.add(new BasicNameValuePair(USER_JSON_KEY, user));
		try {
			AsyncHttpClientUtil.post(getAsyncHttpClient(context),
					getSaveUserUrl(), context, new UrlEncodedFormEntity(data,
							HTTP.UTF_8), handler);
		} catch (final UnsupportedEncodingException e) {
			Toast.makeText(context, "网络连接异常，请检查", Toast.LENGTH_SHORT).show();
		}
	}

	public static void startPostTransferMoneyRequest(
			final String transfersJson,final String id, final String password,
			final AsyncHttpResponseHandler handler, final Context context) {
		final List<NameValuePair> data = new ArrayList<NameValuePair>();
		data.add(new BasicNameValuePair(TRANSFER_KEY, transfersJson));
		data.add(new BasicNameValuePair(UUID_KEY, id));
		data.add(new BasicNameValuePair(PASSWORD_KEY, password));
		try {
			AsyncHttpClientUtil.post(getAsyncHttpClient(context),
					getTransferMoneyUrl(), context, new UrlEncodedFormEntity(
							data, HTTP.UTF_8), handler);
		} catch (final UnsupportedEncodingException e) {
			Toast.makeText(context, "网络连接异常，请检查", Toast.LENGTH_SHORT).show();
		}
	}

	public static void startPostBindCardRequest(final String cardJson,
			final AsyncHttpResponseHandler handler, final Context context) {
		final List<NameValuePair> data = new ArrayList<NameValuePair>();
		data.add(new BasicNameValuePair(CARD_JSON_KEY, cardJson));
		try {
			AsyncHttpClientUtil.post(getAsyncHttpClient(context),
					getBindCardUrl(), context, new UrlEncodedFormEntity(data,
							HTTP.UTF_8), handler);
		} catch (final UnsupportedEncodingException e) {
			Toast.makeText(context, "网络连接异常，请检查", Toast.LENGTH_SHORT).show();
		}
	}

	public static void startPostCreatePayCodeRequest(final String codeInfo,
			final AsyncHttpResponseHandler handler, final Context context) {
		final List<NameValuePair> data = new ArrayList<NameValuePair>();
		data.add(new BasicNameValuePair(CODEINFO_KEY, codeInfo));
		try {
			AsyncHttpClientUtil.post(getAsyncHttpClient(context),
					getPayCodeUrl(), context, new UrlEncodedFormEntity(data,
							HTTP.UTF_8), handler);
		} catch (final UnsupportedEncodingException e) {
			Toast.makeText(context, "网络连接异常，请检查", Toast.LENGTH_SHORT).show();
		}
	}

	public static void startPostGetTransferBillRequest(
			final String phonenumber, final AsyncHttpResponseHandler handler,
			final Context context) {
		final List<NameValuePair> data = new ArrayList<NameValuePair>();
		data.add(new BasicNameValuePair(PHONE_NUMBER, phonenumber));
		try {
			AsyncHttpClientUtil.post(getAsyncHttpClient(context),
					getTransfersByUserUrl(), context, new UrlEncodedFormEntity(
							data, HTTP.UTF_8), handler);
		} catch (final UnsupportedEncodingException e) {
			Toast.makeText(context, "网络连接异常，请检查", Toast.LENGTH_SHORT).show();
		}
	}

	public static void startPostGetAllCardsRequest(final String phonenumber,
			final AsyncHttpResponseHandler handler, final Context context) {
		final List<NameValuePair> data = new ArrayList<NameValuePair>();
		data.add(new BasicNameValuePair(PHONE_NUMBER, phonenumber));
		try {
			AsyncHttpClientUtil.post(getAsyncHttpClient(context),
					getAllCardsUrl(), context, new UrlEncodedFormEntity(data,
							HTTP.UTF_8), handler);
		} catch (final UnsupportedEncodingException e) {
			Toast.makeText(context, "网络连接异常，请检查", Toast.LENGTH_SHORT).show();
		}
	}

	public static void startPostUnbindCardRequest(final String cardnumber,
			final AsyncHttpResponseHandler handler, final Context context) {
		final List<NameValuePair> data = new ArrayList<NameValuePair>();
		data.add(new BasicNameValuePair(CARD_NUMBER, cardnumber));
		try {
			AsyncHttpClientUtil.post(getAsyncHttpClient(context),
					getUnbindCardUrl(), context, new UrlEncodedFormEntity(data,
							HTTP.UTF_8), handler);
		} catch (final UnsupportedEncodingException e) {
			Toast.makeText(context, "网络连接异常，请检查", Toast.LENGTH_SHORT).show();
		}
	}
	public static void startPostRechargeRequest(final String rechargeJson,
			final AsyncHttpResponseHandler handler, final Context context) {
		final List<NameValuePair> data = new ArrayList<NameValuePair>();
		data.add(new BasicNameValuePair(RECHARGE_KEY, rechargeJson));
		try {
			AsyncHttpClientUtil.post(getAsyncHttpClient(context),
					getRechargeUrl(), context, new UrlEncodedFormEntity(data,
							HTTP.UTF_8), handler);
		} catch (final UnsupportedEncodingException e) {
			Toast.makeText(context, "网络连接异常，请检查", Toast.LENGTH_SHORT).show();
		}
	}
	
	public static void startPostGetBillsRequest(String phonenumber,
			final AsyncHttpResponseHandler handler, Context context) {
		final List<NameValuePair> data = new ArrayList<NameValuePair>();
		data.add(new BasicNameValuePair(PHONE_NUMBER, phonenumber));
		try {
			AsyncHttpClientUtil.post(getAsyncHttpClient(context),
					getBillsUrl(), context, new UrlEncodedFormEntity(data,
							HTTP.UTF_8), handler);
		} catch (final UnsupportedEncodingException e) {
			Toast.makeText(context, "网络连接异常，请检查", Toast.LENGTH_SHORT).show();
		}
	}

	public static AsyncHttpClient getAsyncHttpClient(final Context context) {
		final AsyncHttpClient ASYNC_HTTP_CLIENT = new AsyncHttpClient();
		ASYNC_HTTP_CLIENT.setTimeout(8000);
		final HttpParams params = ASYNC_HTTP_CLIENT.getHttpClient().getParams();
		params.setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
		return ASYNC_HTTP_CLIENT;
	}

	public static void startGetSweepCode(final String url,
			final AsyncHttpResponseHandler handler, final Context context) {
		AsyncHttpClientUtil.get(getAsyncHttpClient(context), url, context,
				handler);
	}
}
